package com.hxz.graduationdesign.service.impl;

import com.hxz.graduationdesign.mapper.DanmuMapper;
import com.hxz.graduationdesign.pojo.entity.Danmu;
import com.hxz.graduationdesign.pojo.vo.DanmuVo;
import com.hxz.graduationdesign.service.IDanmuService;
import com.hxz.graduationdesign.util.ChineseSegmenter;
import com.hxz.graduationdesign.util.SentimentAnalyzer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

import static com.hxz.graduationdesign.util.BilibiliDanmuCrawler.getDanmu;
import static com.hxz.graduationdesign.util.FormattingTool.getChineseSegmenter;
import static com.hxz.graduationdesign.util.FormattingTool.getSentimentAnalyzer;

@Service
@Slf4j
public class DanmuService implements IDanmuService {

    @Autowired
    private DanmuMapper danmuMapper;

    @Override
    public List<DanmuVo> query(String bv) {
        List<DanmuVo> danmuVos = danmuMapper.select(bv);
        if (danmuVos.isEmpty()){

            List<Danmu> danmus = getDanmu(bv);
            log.debug("添加弹幕bv："+bv);
            if(danmus.isEmpty()){
                return new ArrayList<>();
            }

            int i = add(danmus);
            log.debug("添加弹幕数量："+i);
            if(i>0){
                return danmuMapper.select(bv);
            }else {
                return null;
            }

        }
        log.debug("查询弹幕bv："+bv);
        return danmuVos;
    }

    @Override
    public int add(List<Danmu> danmus) {
        return danmuMapper.insert(danmus);
    }

    @Override
    public List<DanmuVo> update(String bv) {

        danmuMapper.deleteByBv(bv);
        return query(bv);

    }

    @Override
    public Map<String, Integer> segmenter(String bv) {
        List<DanmuVo> query = query(bv);
        List<String> danmu = new ArrayList<>();
        for (DanmuVo danmuVo : query) {
            danmu.add(danmuVo.getDanmu());
        }

        try {
            ChineseSegmenter chineseSegmenter = getChineseSegmenter();
            Map<String, Integer> stringIntegerMap = chineseSegmenter.batchSegment(danmu);
            Map<String, Integer> topResult = new LinkedHashMap<>();
            int count = 0;
            for (Map.Entry<String, Integer> entry : stringIntegerMap.entrySet()) {
                if (count >= 50) {
                    break;
                }
                topResult.put(entry.getKey(), entry.getValue());
                count++;
            }
            return topResult;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public Map<String, Integer> emo(String bv) {
        SentimentAnalyzer sentimentAnalyzer = getSentimentAnalyzer();
        List<DanmuVo> query = query(bv);
        List<String> danmus = new ArrayList<>();
        for (DanmuVo danmuVo : query) {
            danmus.add(danmuVo.getDanmu());
        }
        Map<String,Integer> emo = new HashMap<>();
        emo.put("中立",0);
        emo.put("消极",0);
        emo.put("积极",0);
        for (String danmu : danmus) {
            String analyze = sentimentAnalyzer.analyze(danmu);
            if (analyze.equals("中立")){
                emo.compute("中立", (k, v) -> v + 1);
            } else if (analyze.equals("积极")) {
                emo.compute("积极", (k, v) -> v + 1);
            }else if (analyze.equals("消极")){
                emo.compute("消极", (k, v) -> v + 1);
            }
        }

        return emo;
    }
}
